#!/usr/bin/env python

#    Copyright 2011,2012 Erik Persson
#
#    This file is part of the cell-sync-usrp project.
#
#    cell-sync-usrp is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    cell-sync-usrp is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with cell-sync-usrp.  If not, see <http://www.gnu.org/licenses/>.


##################################################
# Gnuradio Python Flow Graph
# Title: Top Block
# Generated: Wed Mar 30 09:06:37 2011
##################################################

from gnuradio import eng_notation
from gnuradio import gr
from gnuradio import uhd
from gnuradio.eng_option import eng_option
from gnuradio.gr import firdes
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import wx
import time

class top_block(grc_wxgui.top_block_gui):

    def __init__(self):
        grc_wxgui.top_block_gui.__init__(self, title="Top Block")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 320000
        self.freq = freq = 106.3e6

        ##################################################
        # Blocks
        ##################################################
        self.uhd_usrp_source_0 = uhd.usrp_source(
            device_addr="",
            io_type=uhd.io_type.COMPLEX_FLOAT32,
            num_channels=1,
        )
        self.uhd_usrp_source_0.set_samp_rate(samp_rate)
        self.uhd_usrp_source_0.set_center_freq(freq, 0)
        self.uhd_usrp_source_0.set_gain(40, 0)
        self.gr_rms_xx_0 = gr.rms_cf(0.13)
        self.gr_probe_signal_f_0 = gr.probe_signal_f()
        self.gr_nlog10_ff_0 = gr.nlog10_ff(25, 1, 0)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.uhd_usrp_source_0, 0), (self.gr_rms_xx_0, 0))
        self.connect((self.gr_rms_xx_0, 0), (self.gr_nlog10_ff_0, 0))
        self.connect((self.gr_nlog10_ff_0, 0), (self.gr_probe_signal_f_0, 0))

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.uhd_usrp_source_0.set_samp_rate(self.samp_rate)

    def get_freq(self):
        return self.freq

    def set_freq(self, freq):
        self.freq = freq
        self.uhd_usrp_source_0.set_center_freq(self.freq, 0)


if __name__ == '__main__':
    parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
    (options, args) = parser.parse_args()
    tb = top_block()
    
    print "starting"
    #tb.start()
    tb.start()
    #tb.set_volume(10)

    start_freq =  2112.5e6
    end_freq = 2170e6
    freq_step = 5.0e6

    tb.set_freq(start_freq)
    tb.set_samp_rate(10e6)
    maxLevel = -1000000000.0
   
    maxFreq = 0.0
    time.sleep(1)
    try:
        """tb.set_freq(103.3);
        while (True):
            a=0
            value=0
            while (a<100):
                a=a+1
                value += tb.gr_probe_avg_mag_sqrd_x_0.level() / 100.0
                
            print value
        """
        while (tb.get_freq()<end_freq):
            a=0
            value = 0.0
            while (a<100):
                a=a+1
                value += tb.gr_probe_signal_f_0.level() / 100.0
                
            if (value > maxLevel):
                maxFreq = tb.get_freq()
                maxLevel = value
                    
            if (value > -95):
                print "found channel: "
                print (tb.get_freq())
                print (value)
            
            tb.set_freq(tb.get_freq()+freq_step)
            #time.sleep(0.01)
        print "Scan completed"
        print "freq, level"
        print maxLevel
        print maxFreq
            
    except(KeyboardInterrupt):
        print "test"
    
    
    tb.stop()
    print "stopping"
    
    
        
        
